package com.kingsoft.dc.khaos.plugin.core.utils;

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jing on 19/6/24.
 */
public class UserConfig {
    Map userConfigMap = new HashMap<>();

    public UserConfig() {
        parseYaml("user_config.yml");
    }

    private void parseYaml(String fileName) {
        Yaml yaml = new Yaml();
        InputStream inputStream = UserConfig.class.getClassLoader().getResourceAsStream(fileName);
        this.userConfigMap =  yaml.load(inputStream);
    }

    public Object getValueByKey(String root, String... keys) {
        Map<String, Object> rootProperty = (Map)userConfigMap.get(root);
        List<String> keyList = Arrays.asList(keys);
        List<String> middleKeys = keyList.subList(0, keyList.size() - 1);
        String leafKey = keyList.get(keyList.size() - 1);
        for (String key : middleKeys) {
            Map<String, Object> property = (Map)rootProperty.get(key);
            rootProperty = property;
        }

        return rootProperty.getOrDefault(leafKey, null);
    }

}
